<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  news
 */

class m_news_documents_Item extends m_news_documents_ItemBase
{
	/**
	 * @return m_news_documents_Item
	 */
	public static function createNew()
	{
		return new self();
	}
	
	/**
	 * @return m_news_documents_Item
	 */
	public static function getInstanceByUniqueId($id, $lang = null)
	{
		if(is_null($lang)) $lang = f_core_Context::getInstance()->getLang();
		$cache = f_core_Cache::getInstance();
		
		$key = $cache->getDocumentCacheKey($id, $lang);
		
		if($cache->hasCache($key))
		{
			return $cache->getCacheContent($key);
		}
		
		$object = new self($id, $lang);
		
		$cache->putInCache($key, $object);
		
		return $object;
	}

    public function getFrontHeader($length = 100)
    {
        $header = $this->getHeader();

        if(is_null($header) || $header == '')
        {
            $header = f_utils_String::truncateAtSentence(strip_tags($this->getContent()), $length, '.');
        }

        return $header;
    }

	public function getEditUrl($lang = null, $tabName = null)
	{
		if(is_null($lang))
		{
			$lang = f_core_Request::getInstance()->getParameter("docLang");
		}
				
		return f_core_Routing::getUrl("editNews", array("docId" => $this->getUniqueId(), "docLang" => strtolower($lang)));
	}
	
	public function getShowUrl($lang = null)
	{
		if(is_null($lang))
		{
			$lang = f_core_Request::getInstance()->getParameter("docLang");
		}

		return f_core_Routing::getUrl("showNews", array("docId" => $this->getUniqueId(), "docLang" => strtolower($lang)));
	}
	
	public function canSlide()
	{
		return false;
	}

    public function getMenuLabel()
    {
        return $this->getLabel();
    }
	
	public function getPictureUrl($width=50, $height=50)
	{
	    $media = '';
	    if(!is_null($this->getPictures()) && is_string($this->getPictures()) && $this->getPictures() != '' && $this->getPictures() != "0")
	    {
            $docProvider = f_document_Provider::getInstance();
            if($docProvider->checkDocument($this->getPictures(), $this->getLang()))
            {
                $pic = f_document_Provider::getInstance()->getByModelAndUniqueId('medias/media', $this->getPictures(), $this->getLang())->getImageUrlByMaxSize($width, $height);

                if(!is_null($pic)) $media = $pic;
            }
	    }
	    
		return $media;
	}

    public function getFrontUrls($folder = null)
    {
        $pages = $this->getDisplayPages($folder);

        $result = array();

        if(count($pages) > 0)
        {
            foreach($pages as $page)
            {
                $res['url'] = m_website_services_PageService::getInstance()->getDynamicUrl($this, $page);
                $res['page'] = $page;
                $result[] = $res;
            }
        }

        return $result;
    }

    public function getPreviewUrl()
    {
        $documentProvider = f_document_Provider::getInstance();
        $dbProvider = f_database_Provider::getInstance();

        $rlManager = f_relation_Manager::getInstance();

        $parent = $rlManager->getUniqueParent($this->getUniqueId());

        if($parent != null)
        {
            $pages = m_website_services_PageService::getInstance()->searchPagesByBlockParameters(array('action' => 'front_News', 'module'=>'news', 'folderNewsId'=>$parent->getUniqueId()));

            $result = array();

            if(count($pages) > 0)
            {
                return m_website_services_PageService::getInstance()->getDynamicUrl($this, $pages[0]).'?preview=1';
            }
        }

        return '';
    }

    public function getBoSearchIcon()
    {
        return m_medias_services_MediaService::getModuleMediaPath('news', 'news.png');
    }
}